package com.sangmo.fts.api.inventory;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.inventory.interfaces.IfLadingBillCreation;
import com.sangmo.fts.process.inventory.model.IfLadingBill;
import com.sangmo.fts.process.inventory.model.IfLadingBillItem;
import com.sangmo.fts.process.inventory.service.IfLadingBillService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/ifLadingBill/v1")
@Access("ifLadingBill")
public class IfLadingBillApi {

    @Autowired
    private IfLadingBillService ifLadingBillService;

    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<IfLadingBill> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid IfLadingBillCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(ifLadingBillService.create(obj));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<IfLadingBill>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("IfLadingBill.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ifLadingBillService.search(ps));
    }

    @Access(":remove")
    @PostMapping("/{trainingId}/{id}/remove")
    public @ResponseBody ApiResponse remove(@PathVariable("trainingId") Long trainingId,@PathVariable("id") Long id) {
        ifLadingBillService.deleteById(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.IfProcessNodes.IF_LADING_BILL_SELLER_APPLY_12)
    @Access(":commit")
    @PostMapping(value = "/{trainingId}/{id}/commit")
    public @ResponseBody
    ApiResponse audit(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ifLadingBillService.commit(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/items/list")
    public @ResponseBody ApiResponse<PageResult<IfLadingBillItem>> listGoodsItems(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long loanId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        return ApiResponse.make(ifLadingBillService.listItemsByPage(loanId, ps));
    }

    @TrainingProcess(node = TrainingHistory.IfProcessNodes.IF_LADING_BILL_BANK_CONFIRM_13)
    @Access(":confirm")
    @PostMapping(value = "/{trainingId}/{id}/confirm")
    public @ResponseBody
    ApiResponse confirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ifLadingBillService.confirm(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.IfProcessNodes.IF_LADING_BILL_STORAGE_COMPLETE_14)
    @Access(":complete")
    @PostMapping(value = "/{trainingId}/{id}/complete")
    public @ResponseBody
    ApiResponse complete(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ifLadingBillService.complete(id);
        return ApiResponse.make();
    }

    @Access(":selection")
    @PostMapping({"/{trainingId}/selection"})
    @ResponseBody
    public ApiResponse<PageResult<IfLadingBill>> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("IfLadingBill.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ifLadingBillService.selection(ps));
    }
}
